package io.xxx.xbutler.core.callback;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.hy.corecode.idgen.WFGIdGenerator;
import io.xxx.xbutler.data.RobotRoomMapper;
import io.xxx.xbutler.domain.Callback;
import io.xxx.xbutler.domain.RobotRoom;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

/**
 * 机器邀请好友入群回调接口（兼容PC）<br/>
 * 机器人邀请好友入群操作回调，成功与失败仅代表机器人的邀请状态，不代表好友是否进入了群，
 * 当机器人的好友把机器人删除或好友账号异常或被封号等异常情况，回调中该好友无法进入群。
 *
 * <pre>
 * {
 *     "nType": 3006,
 *     "vcMerchantNo": "",
 *     "vcRobotWxId": "",
 *     "vcRobotSerialNo": "",
 *     "vcSerialNo": "",
 *     "nResult": 1,
 *     "vcResult": "SUCCESS",
 *     "Data": {
 *         "vcFriendWxId": "",
 *         "vcFailFriendWxId": "",
 *         "vcFriendSerialNo": "",
 *         "vcChatRoomId": "",
 *         "vcChatRoomSerialNo": "",
 *         "dtCreateTime": "",
 *         "vcFriendData": {
 *             "InGroup": [
 *                 {
 *                     "vcFriendWxId": "",
 *                     "vcFriendSerialNo": ""
 *                 }
 *             ],
 *             "NotGroup": [
 *                 {
 *                     "vcFriendWxId": "",
 *                     "vcFriendSerialNo": ""
 *                 }
 *             ],
 *             "InRoom": [],
 *             "NoFriend": [],
 *             "Invite": "",
 *             "InviteMembers": []
 *         }
 *     },
 *     "nTimeStamp": 1612249038469
 * }
 * </pre>
 */
@Slf4j
@Component
public class _3006Handler implements CallbackHandler {

    @Resource
    private WFGIdGenerator idGenerator;

    @Resource
    private RobotRoomMapper robotRoomMapper;

    @Override
    public String name() {
        return "机器邀请好友入群回调";
    }

    @Override
    public boolean supports(int type) {
        return type == 3006;
    }

    @Override
    public void handle(Callback callback) {
        LocalDateTime now = LocalDateTime.now();
        String robotId = callback.getRobotId();
        String robotSerialNo = callback.getRobotSerialNo();
        JSONObject data = (JSONObject) callback.getData();
        String roomId = data.getString("vcChatRoomId");
        String roomSerialNo = data.getString("vcChatRoomSerialNo");
        Instant invitationTime = data.getInstant("dtCreateTime");
        JSONObject friendData = data.getJSONObject("vcFriendData");
        JSONArray inGroup = friendData.getJSONArray("InGroup");
        List<RobotRoom> robotRooms = inGroup.stream()
                .map(it -> {
                    JSONObject friend = JSONObject.from(it);
                    String friendId = friend.getString("vcFriendWxId");
                    String friendSerialNo = friend.getString("vcFriendSerialNo");

                    RobotRoom robotRoom = new RobotRoom();
                    robotRoom.setId(idGenerator.next());
                    robotRoom.setRobotId(friendId);
                    robotRoom.setRobotSerialNo(friendSerialNo);
                    robotRoom.setRoomId(roomId);
                    robotRoom.setRoomSerialNo(roomSerialNo);
                    robotRoom.setInviterId(robotId);
                    robotRoom.setInviterSerialNo(robotSerialNo);
                    robotRoom.setInvitationTime(LocalDateTime.ofInstant(invitationTime, ZoneId.systemDefault()));
                    robotRoom.setCreatedTime(now);
                    robotRoom.setUpdatedTime(now);
                    return robotRoom;
                })
                .toList();
        robotRoomMapper.insert(robotRooms);
    }
}
